#include "pluginadminservice.h"
#include <service/event/ctkEventConstants.h>
#include <QJsonArray>
#include <QThread>
#include <ctkPluginException.h>

PluginAdminService::PluginAdminService(ctkPluginContext *context, QObject *parent) :
    QObject(parent),
    m_context(context),
    m_eventAdmin(nullptr),
    m_framework(nullptr)
{
    qDebug() << Q_FUNC_INFO << QThread::currentThread();
    //注册服务
    context->registerService<PluginAdmin>(this);
    //EventAdmin服务
    ctkServiceReference eventRef = context->getServiceReference<ctkEventAdmin>();
    m_eventAdmin = qobject_cast<ctkEventAdmin*>(context->getService(eventRef));
    if(eventRef){

        context->ungetService(eventRef);
    }
}

PluginAdminService::~PluginAdminService()
{

}

void PluginAdminService::closeAllPlugin()
{
    foreach (const QSharedPointer<ctkPlugin> &plugin, m_context->getPlugins()) {
        try {
            if("PluginAdmin" != plugin->getSymbolicName()
                    && "system.plugin" != plugin->getSymbolicName())
            {
                qDebug() << "卸载插件：" << plugin->getSymbolicName();
                plugin->stop();
                plugin->uninstall();
            }
        }
        catch (const ctkPluginException& exc) {
            qCritical() << exc.what();
        }
    }

    foreach (const QSharedPointer<ctkPlugin> &plugin, m_context->getPlugins()) {
        try {
            if("PluginAdmin" == plugin->getSymbolicName())
            {
                qDebug() << "卸载插件：" << plugin->getSymbolicName();
                plugin->stop();
                plugin->uninstall();
            }
        }
        catch (const ctkPluginException& exc) {
            qCritical() << exc.what();
        }
    }
}

void PluginAdminService::setPluginFramework(const QSharedPointer<ctkPluginFramework>& framework)
{
    m_framework = framework;
}

















